package com.tj.jinzao.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.widgets.ZoomImageView;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 查看图片
 * Created by wjy on 2019/1/5.
 */
public class ImageActivity extends BaseActivity {

    public static final String EXTRA_IMAGE_URL = "url";
    public static final String EXTRA_IMAGE_URLS = "urlArray";
    public static final String EXTRA_IMAGE_TEXTS = "urlText";

    @BindView(R.id.pager)
    ViewPager mViewPager;
    @BindView(R.id.tv_desc)
    TextView mTextDescView;
    private ArrayList<String> mUrls;
    private ArrayList<String> mTexts;
    private String mUrl;
    private int mPagerPosition;


    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUrl = getIntent().getStringExtra(EXTRA_IMAGE_URL);
        mUrls = getIntent().getStringArrayListExtra(EXTRA_IMAGE_URLS);
        mTexts = getIntent().getStringArrayListExtra(EXTRA_IMAGE_TEXTS);
        if (TextUtils.isEmpty(mUrl) && mUrls == null) {
            finish();
            return;
        }
        mPagerPosition = mUrls.indexOf(mUrl);
        mViewPager = findViewById(R.id.pager);
        ImagePagerAdapter mAdapter = new ImagePagerAdapter();
        mViewPager.setAdapter(mAdapter);
        mTextDescView = findViewById(R.id.tv_desc);
        if (mTexts != null && mTexts.size() > 0) {
            mTextDescView.setText(mTexts.get(0));
            mTextDescView.setVisibility(View.VISIBLE);
        } else {
            mTextDescView.setVisibility(View.GONE);
        }


        CharSequence text = "预览(1 / " + (mViewPager.getAdapter().getCount()) + ")";

        setTitleText(text);

        // 更新下标
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                if (mTexts != null && mTexts.size() > 0) {
                    mTextDescView.setText(mTexts.get(arg0));
                }
                CharSequence text = "预览(" + (arg0 + 1) + " / " + (mViewPager.getAdapter().getCount()) + ")";
                setTitleText(text);
            }

        });
        pos=getIntent().getIntExtra("pos",0);
        mViewPager.setCurrentItem(pos);

//        mViewPager.setCurrentItem(mPagerPosition);

    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_view_image;
    }
    private int pos;

    public static void start(Context context, String url, ArrayList<String> urls, int pos,ArrayList<String> texts) {

        Intent intent = new Intent(context, ImageActivity.class);
        intent.putExtra(EXTRA_IMAGE_URL, url);
        intent.putExtra("pos",pos);
        intent.putStringArrayListExtra(EXTRA_IMAGE_URLS, urls);
        intent.putStringArrayListExtra(EXTRA_IMAGE_TEXTS, texts);
        context.startActivity(intent);
    }


    private class ImagePagerAdapter extends PagerAdapter {

        private ImagePagerAdapter() {
        }

        @Override
        public int getCount() {
            return mUrls == null ? 0 : mUrls.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object o) {
            return view == o;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }


        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            ZoomImageView imageView = new ZoomImageView(container.getContext());
            Glide.with(ImageActivity.this).load(mUrls.get(position)).into(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageActivity.this.finish();
                }
            });
            container.addView(imageView, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
            return imageView;
        }
    }
}
